package com.zt.weibo.utils;

import android.text.format.Time;

import com.zt.weibo.vo.UserInfo;

/**
 * 字符串格式化类
 * @author zhaotong
 */
public class FormatString
{
	/**
	 * 将JSON中“created_at”对应字符串格式化为YYYY-MM-DD  HH:MM:SS  WEEK的格式
	 * @param str JSON中“created_at”对应字符串
	 * @return 格式化后字符串
	 */
	public static String formatCreatTime(String str)
	{
		String strTemp = "";
		StringBuffer strCreatTimeBuffer = new StringBuffer();
		strTemp = str.substring(str.length() - 4, str.length());
		strCreatTimeBuffer.append(strTemp + "-");
		strTemp = str.substring(4, 7);
		if("Jan".equals(strTemp))
		{
			strCreatTimeBuffer.append("01-");
		}
		else if ("Feb".equals(strTemp))
		{
			strCreatTimeBuffer.append("02-");
		}
		else if ("Mar".equals(strTemp))
		{
			strCreatTimeBuffer.append("03-");
		}
		else if ("Apr".equals(strTemp))
		{
			strCreatTimeBuffer.append("04-");
		}
		else if ("May".equals(strTemp))
		{
			strCreatTimeBuffer.append("05-");
		}
		else if ("Jun".equals(strTemp))
		{
			strCreatTimeBuffer.append("06-");
		}
		else if ("Jul".equals(strTemp))
		{
			strCreatTimeBuffer.append("07-");
		}
		else if ("Aug".equals(strTemp))
		{
			strCreatTimeBuffer.append("08-");
		}
		else if ("Sep".equals(strTemp))
		{
			strCreatTimeBuffer.append("09-");
		}
		else if ("Oct".equals(strTemp))
		{
			strCreatTimeBuffer.append("10-");
		}
		else if ("Nov".equals(strTemp))
		{
			strCreatTimeBuffer.append("11-");
		}
		else if ("Dec".equals(strTemp))
		{
			strCreatTimeBuffer.append("12-");
		}
		strTemp = str.substring(8, 10);
		strCreatTimeBuffer.append(strTemp + "  ");
		strTemp = str.substring(11, 19);
		strCreatTimeBuffer.append(strTemp + "  ");
		strTemp = str.substring(0, 3);
		if("Mon".equals(strTemp))
		{
			strCreatTimeBuffer.append("周一");
		}
		else if ("Tue".equals(strTemp))
		{
			strCreatTimeBuffer.append("周二");
		}
		else if ("Wednesday".contains(strTemp))
		{
			strCreatTimeBuffer.append("周三");
		}
		else if ("Thursday".contains(strTemp))
		{
			strCreatTimeBuffer.append("周四");
		}
		else if ("Friday".contains(strTemp))
		{
			strCreatTimeBuffer.append("周五");
		}
		else if ("Saturday".contains(strTemp))
		{
			strCreatTimeBuffer.append("周六");
		}
		else if ("Sunday".contains(strTemp))
		{
			strCreatTimeBuffer.append("周日");
		}
		return strCreatTimeBuffer.toString();
	}
	
	/**
	 * 将JSON中“source”对应字符串去掉网址保留名称
	 * @param str JSON中“source”对应字符串
	 * @return 来源名称
	 */
	public static String formatSource(String str)
	{
		StringBuffer strSourceBuffer = new StringBuffer();
		str = str.substring(str.indexOf(">") + 1, str.lastIndexOf("<"));
		strSourceBuffer.append(str);
		return strSourceBuffer.toString();
	}
	
	/**
	 * 根据是否具有备注信息产生不同的昵称字符串
	 * @param tempInfo 用户信息类
	 * @return 昵称字符串
	 */
	public static String formatRemark(UserInfo tempInfo)
	{
		StringBuffer strRemarkBuffer = new StringBuffer();
		if("".equals(tempInfo.strRemark))
		{
			strRemarkBuffer.append(tempInfo.strName + ":");
		}
		else
		{
			strRemarkBuffer.append(tempInfo.strRemark + ":");
		}
		return strRemarkBuffer.toString();
	}
	
	/**
	 * 转换expiresin为过期时间
	 * @param expiresin
	 * @return int的过期时间，存进数据库。
	 */
	public static int formatInvalidTime(String expiresin)
	{
		int iExpiresin = -1;
		try
		{
			iExpiresin = Integer.parseInt(expiresin);
		}
		catch (NumberFormatException e)
		{
			e.printStackTrace();
		}
		Time time = new Time();
		time.setToNow();
		int invaltime = (int)(time.toMillis(true) / 1000) + iExpiresin;
		Time invalDate = new Time();
		invalDate.set((long)invaltime * 1000);
		PrintLog.debug("过期时间 = " + invalDate.format2445() + 
				"\n 当前时间 = " + time.format2445());
		return invaltime;
	}
	
}
